package com.android.support.lib.universal.sdk.weixin;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.general.Session;
import com.android.support.lib.universal.http.NotifyUrl;
import com.android.support.lib.universal.listenner.QrBaseCallBack;
import com.android.support.lib.universal.sdk.weixin.base.CodeWXPayEntry;
import com.android.support.lib.universal.sdk.weixin.model.WXuser;
import com.android.support.lib.universal.sdk.weixin.wxapi.WeixinManager;
import com.android.support.lib.universal.sdk.weixin.wxapi.WeixinManager.WeixinManagerListener;
import com.android.support.lib.universal.ui.UiHelper;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import cn.gamehot.mobile.wxapi.WXPayEntryActivity;


/**
 * 微信测试案例
 *
 * @author 彭治铭
 */
public class DemoWXActivity extends Activity implements OnClickListener {
    ImageView img_code;// 二维码

    CodeWXPayEntry codePay;// 二维码支付

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_activity_sdk_weixin);

        img_code = (ImageView) findViewById(R.id.img_code);
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.btn_weixin_pay) {// App支付
            NotifyUrl.setOutTradeNo(this, System.currentTimeMillis() + "");// 存储订单号，必不可少
            WeixinManager.getInstance().pay("商品名", "0.01");
            Intent intent = new Intent();
            intent.setClass(this, WXPayEntryActivity.class);
            startActivity(intent);
            WeixinManager.getInstance().setWeixinmanagerListener(
                    new WeixinManagerListener() {
                        @Override
                        public void onPaySuccess() {
                            Log.e("test", "成功");
                        }

                        @Override
                        public void onPayFailed() {
                            Log.e("test", "失败");
                        }
                    });
        }

        if (v.getId() == R.id.btn_weixin_code) {// 生成二维码
            codePay = new CodeWXPayEntry();
            codePay.onPay(this, System.currentTimeMillis() + "", "商品名", "0.01",
                    new QrBaseCallBack() {

                        @Override
                        public void onSuccess(String result, Bitmap bitmap) {
                            // TODO Auto-generated method stub
                            img_code.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onPaySuccess() {
                            // TODO Auto-generated method stub
                            Log.e("test", "支付成功");
                        }

                        @Override
                        public void onFailure(String result) {
                            // TODO Auto-generated method stub
                            Log.e("test", result);
                        }

                        @Override
                        public void onCancel() {
                            // TODO Auto-generated method stub
                            Log.e("test", "支付取消");
                        }
                    });
        }

        if (v.getId() == R.id.btn_weixin_cancel) {// 订单轮询取消
            if (codePay != null) {
                codePay.onCancel();
            }
        }

        if (v.getId() == R.id.btn_weixin_logo) {// 微信登录
            UiHelper.toWeixin(this);
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        WXuser xuser = (WXuser) Session.getSession().get("WXuser");
        if (xuser != null) {
            Toast.makeText(this, "授权成功", Toast.LENGTH_LONG).show();
        }
    }

}
